package com.feng.controller;

import com.feng.entity.Menu;
import com.feng.entity.RestResponse;
import com.feng.entity.User;
import com.feng.service.MenuService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/menu")
public class MenuController {

    final
    MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @GetMapping("/getRouters")
    public RestResponse getRouters(){
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String  id = user.getAccount().getId();
        return menuService.selectRouterMenuTreeByUserId(id);
    }

    @GetMapping("/treeSelect")
    public RestResponse treeSelect(){
        return menuService.selectMenuTree();
    }

    @PreAuthorize("@ps.hasPermission('system:menu:query')")
    @GetMapping("/list")
    public RestResponse listAll(String status, String menuName){
        return menuService.listAll(status, menuName);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:add')")
    @PostMapping("/addMenu")
    public RestResponse addMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:query')")
    @GetMapping("/getMenuById/{id}")
    public RestResponse getMenuById(@PathVariable Long id){
        return menuService.getMenuById(id);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:edit')")
    @PutMapping("/updateMenu")
    public RestResponse updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:remove')")
    @DeleteMapping("/deleteMenu/{id}")
    public RestResponse deleteMenu(@PathVariable Long id){
        return menuService.deleteMenu(id);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        menuService.export(response);
    }
}
